package com.medusa.aps.business.modules.basic.model.input;

import com.medusa.aps.business.global.model.exception.GlobalException;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

/**
 * 班制参数
 *
 * @author shenzhiwei
 */
@Data
@Accessors(chain = true)
@Schema(description = "班制参数")
public class ClassSystemParam {

    /**
     * 主键
     */
    @Schema(title = "id", description = "主键,新增时不传，修改时必传", example = "1")
    @NotNull(message = "id不能为空", groups = {Update.class})
    private Integer id;

    /**
     * 工作日与工作时段关联参数
     */
    @NotNull
    @Valid
    @Size(min = 7, max = 7, message = "班制名称长度为1-20")
    @Schema(title = "工作日与工作时段集合", description = "工作日与工作时段集合 工作日不允许重复")
    private SortedSet<WorkingDayPeriodParam> workingDayPeriods;
    /**
     * 设备组id集合
     */
    @Schema(description = "设备组id集合", example = "[1,2]")
    @NotNull(message = "请选择设备组")
    @Size(min = 1, message = "请至少选择一个设备组")
    private Set<Long> equipmentIds;

    public List<Integer> periodIds() {
        SortedSet<WorkingDayPeriodParam> workingDayPeriods = this.getWorkingDayPeriods();
        List<Integer> periodIds = new ArrayList<>();
        for (WorkingDayPeriodParam workingDayPeriod : workingDayPeriods) {
            periodIds.add(workingDayPeriod.getPeriodId());
        }
        return periodIds;
    }

    /**
     * 不能全部是休息
     */
    public void valid() {
        if (workingDayPeriods.stream().allMatch(period -> period.getPeriodId() == 0)) {
            throw new GlobalException("至少要选择一个工作时段");
        }
    }

}
